-- trap_kill
-- Created by chengb Sep/11/2015
-- 成就：使用陷阱一回合击杀{count}个以上的敌人

-- 内部函数声明
local whenTrapAction;

-- 关注使用陷阱的事件
EventMgr.register("game_achievement/trap_kill", event.LAUNCH_TRAP, function(args)
    whenTrapAction(args);
end);

-- 怪物死亡的回调
whenTrapAction = function(args)
    local killCount = args.killCount;
    if killCount <= 0 then
        -- 没有击杀任何怪物
        return;
    end

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end
        
        local config = GameAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["count"]) == 'number' and
                killCount < ruleArg["count"] then
                -- 击杀数量未达标
                return false;
            end
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("trap_kill");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, 1);
        end
    end
end


return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);
        local ruleArg  = config["rule_arg"];
        local count    = ruleArg["count"];
        local desc     = config["desc"];

        desc = string.gsub(desc, "{count}", count);

        return desc;
    end,
};